//
//  TickersHeader.m
//  UEAssistant
//
//  Created by mac on 2017/7/28.
//  Copyright © 2017年 youyixinxi. All rights reserved.
//

#import "TickersHeader.h"

@implementation TickersHeader{
    BOOL                            _created;/**< 是否创建过 */
    BOOL                            _canFold;/**< 是否可展开 */
    BOOL                            _flag;
    UIImageView                     *_typeImage;/**< 文件类型图片 */
    UILabel                         *_titleLabel;/**< 标题 */
    UILabel                         *_detailLabel;/**< 详细内容 */
    UIImageView                     *_imageView;/**< 收起图标 */
    UIButton                        *_btn;/**< 收起按钮 */
    UIView                          *_line;/**< 下划线 */
    UILabel                         *_showMore;/**< 展开详情 */
    UIImageView                     *_seletImage;/**< 选中勾勾 */
    CGFloat                         _titleHeight;
    UIButton                        *_selectButton;
}

- (void)cellAutoLayouHeight:(NSInteger)height{
    
}

- (void)setFoldSectionHeaderViewWithTitle:(NSString *)title detail:(NSString *)detail section:(NSInteger)section canFold:(BOOL)canFold height:(CGFloat)height{
    _titleHeight = height;
    if (!_created) {
        [self creatUI];
    }
    _titleLabel.text = title;
    _detailLabel.attributedText = [self attributeStringWith:detail];
    _section = section;
    _canFold = canFold;
    if (canFold) {
        _imageView.hidden = NO;
    } else {
        _imageView.hidden = YES;
    }
}

- (NSMutableAttributedString *)attributeStringWith:(NSString *)money {
    NSString *str = [NSString stringWithFormat:@"应付:%@", money];
    NSMutableAttributedString *ats = [[NSMutableAttributedString alloc] initWithString:str];
    NSString *strMo = [NSString stringWithFormat:@"%@",money];
    NSRange range = [str rangeOfString:strMo];
    [ats setAttributes:@{NSForegroundColorAttributeName:RGB(231, 59, 62, 1),NSFontAttributeName:[UIFont systemFontOfSize:15]} range:range];
    return ats;
}

- (void)creatUI {
    _created = YES;
    
    [self.contentView addSubview:self.seletImage];
    [self.contentView addSubview:self.PaidLabel];
    
    //文件类型图片
    _typeImage = [[UIImageView alloc]initWithFrame:CGRectMake(15+35, 10, 50, 50)];
    _typeImage.image = [UIImage imageNamed:@"word"];
    [self.contentView addSubview:_typeImage];
    
    //文件名字
    _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(_typeImage.frame.origin.x+_typeImage.frame.size.width+10, 10, Screen_Width - _typeImage.frame.size.width-70, _titleHeight)];
    _titleLabel.font = [UIFont systemFontOfSize:16];
    _titleLabel.backgroundColor = [UIColor whiteColor];
    _titleLabel.textColor = [UIColor darkGrayColor];
    _titleLabel.numberOfLines = 0;
    [self.contentView addSubview:_titleLabel];
    
    //应付金额
    _detailLabel = [[UILabel alloc] initWithFrame:CGRectMake(_typeImage.frame.origin.x+_typeImage.frame.size.width+10, _titleLabel.frame.origin.y+_titleLabel.frame.size.height+10, 150, 15)];
    _detailLabel.font = [UIFont systemFontOfSize:13];
    _detailLabel.backgroundColor = [UIColor whiteColor];
    _detailLabel.textColor = [UIColor lightGrayColor];
    [self.contentView addSubview:_detailLabel];
    
    //展开按钮
    _btn = [UIButton buttonWithType:UIButtonTypeCustom];
    _btn.frame = CGRectMake(Screen_Width-105, _titleLabel.frame.origin.y+_titleLabel.frame.size.height+10, 70, 20);
    [_btn setTitle:@"收起详情" forState:UIControlStateNormal];
    [_btn setTitleColor:mainColor forState:UIControlStateNormal];
    _btn.titleLabel.font = [UIFont systemFontOfSize:13];
    [_btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:_btn];
    
    //图片
    _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(_btn.frame.origin.x+_btn.frame.size.width, _btn.frame.origin.y+3, 15, 15)];
    _imageView.image = [UIImage imageNamed:@"收起"];
    [self.contentView addSubview:_imageView];
    
    //选择按钮
    _selectButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _selectButton.frame = CGRectMake(0, 0, 40, 40);
    _selectButton.center = CGPointMake(25, 80*Radio/2);
    _selectButton.backgroundColor = [UIColor colorWithWhite:1 alpha:0];
    [_selectButton addTarget:self action:@selector(selectBtCliced:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:_selectButton];
    
    //线
    _line = [[UIView alloc] initWithFrame:CGRectMake(0, _titleHeight + 55 -1, Screen_Width, 1)];
    _line.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1];
    [self.contentView addSubview:_line];
    
    _flag = YES;
}

- (void)setFold:(BOOL)fold {
    _fold = fold;
    if (fold) {
        
        [UIView animateWithDuration:3 animations:^{
//            _line.frame = CGRectMake(0, _typeImage.frame.origin.y+_typeImage.frame.size.height+13, Screen_Width, 0);
        }];
        _imageView.image = [UIImage imageNamed:@"收起"];
        [_btn setTitle:@"收起详情" forState:UIControlStateNormal];
    }
    else {
        [UIView animateWithDuration:3 animations:^{
//            _line.frame = CGRectMake(0, _typeImage.frame.origin.y+_typeImage.frame.size.height+13, Screen_Width, 1);
        }];
        _imageView.image = [UIImage imageNamed:@"展开"];
        [_btn setTitle:@"展开详情" forState:UIControlStateNormal];
    }
}

- (void)selectBtCliced:(UIButton *)sender{

    if ([self.delegate respondsToSelector:@selector(selectTikkets:section:)]) {
        [self.delegate selectTikkets:sender section:_section];
    }
}

#pragma mark -- 折叠协议
- (void)btnClick:(UIButton *)btn {
    if (_canFold) {
        if ([self.delegate respondsToSelector:@selector(foldHeaderInSection:)]) {
            [self.delegate foldHeaderInSection:_section];
        }
    }
}

- (UIImageView *)seletImage{
    
    if (!_seletImage) {
        _seletImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 20, 20)];
        _seletImage.backgroundColor = [UIColor clearColor];
        _seletImage.center = CGPointMake(25, (_titleHeight + 55)/2);
    }
    return _seletImage;
}

- (UILabel *)PaidLabel{

    if (!_PaidLabel) {
        _PaidLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 50, 30)];
        _PaidLabel.center = CGPointMake(30, (_titleHeight + 55)/2);
        _PaidLabel.backgroundColor = [UIColor clearColor];
        _PaidLabel.font = [UIFont boldSystemFontOfSize:FLEXIBLE(15)];
        _PaidLabel.textColor = [UIColor colorWithWhite:0.7 alpha:1];
        _PaidLabel.text = @"已付款";
        _PaidLabel.hidden = YES;
    }
    return _PaidLabel;
}

@end
